/*
 * SecurityUser.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the web-geek, Version 1.0 (the "License");
 *
 */
package org.geek.login.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.geek.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
public class SecurityUser implements UserDetails {

	private static final long serialVersionUID = -7316100873300248462L;
	
	private User user ;
	
	public SecurityUser(User user){
		this.user = user ;
	}

	public Collection<GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>() ;
		list.add(new GrantedAuthority(){
			private static final long serialVersionUID = 2085540786748691170L;
			public String getAuthority() {
				return "ROLE_USER";
			}
		}) ;
		return list;
	}

	public String getPassword() {
		return user.getPassword();
	}

	public String getUsername() {
		return user.getAccount();
	}

	public boolean isAccountNonExpired() {
		return true ;
	}

	public boolean isAccountNonLocked() {
		return true ;
	}

	public boolean isCredentialsNonExpired() {
		return true ;
	}

	public boolean isEnabled() {
		return true ;
	}
	
	public User getUser(){
		return user ;
	}

	@Override
	public int hashCode() {
		return user.getAccount().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof SecurityUser){
			if(user.getAccount().equals(((SecurityUser)obj).getUser().getAccount())){
				return true;
			}
		}
		return false;
	}
}
